package com.tang.framework.core.domain;


import com.tang.common.constant.HttpStatus;
import com.tang.common.exception.eumes.Error;

/**
 * @author tang jun liang
 */
public class HttpResult<T> {

    private final int code;

    private final String msg;

    private final T data;

    private HttpResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private HttpResult(int code) {
        this(code, null, null);
    }

    private HttpResult(T data) {
        this(HttpStatus.SUCCESS, "", data);
    }

    private HttpResult(String msg) {
       this(HttpStatus.SUCCESS, msg, null);
    }

    public static <T> HttpResult<T> ok(){
        return new HttpResult<>(200);
    }

    public static <T> HttpResult<T> ok(T data){
        return new HttpResult<>(data);
    }

    public static <T> HttpResult<T> okMsg(String msg){
        return new HttpResult<>(msg);
    }
    public static <T> HttpResult<T> ok(int code, String msg){
        return new HttpResult<>(code, msg, null);
    }

    public static <T> HttpResult<T> error(){
        return new HttpResult<>(HttpStatus.ERROR);
    }

    public static <T> HttpResult<T> error(Integer code, String msg){
        return new HttpResult<>(code, msg, null);
    }
    public static <T> HttpResult<T> error(String msg){
        return new HttpResult<>(HttpStatus.ERROR, msg, null);
    }

    public static <T> HttpResult<T> error(Error.ErrorCodeMessage errorCodeMessage){
        return new HttpResult<>(errorCodeMessage.getCode(), errorCodeMessage.getMessage(), null);
    }

    public static <T> HttpResult<T> error(Error.ErrorCodeMessage errorCodeMessage, T data){
        return new HttpResult<>(errorCodeMessage.getCode(), errorCodeMessage.getMessage(), data);
    }


    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }
}
